package br.com.poc.pitagorasnumero.view.activity;

import java.util.List;

import roboguice.activity.RoboFragmentActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener;
import android.view.Menu;
import android.view.MenuItem;
import br.com.poc.pitagorasnumero.R;
import br.com.poc.pitagorasnumero.constant.IdViewConst;
import br.com.poc.pitagorasnumero.constant.TagConst;
import br.com.poc.pitagorasnumero.view.adapter.PitagorasDetailPagerAdapter;
import br.com.poc.pitagorasnumero.view.dto.ResultPitagorasDTO;

import com.google.inject.Inject;

/**
 * The Class NameResultActivity.
 * 
 * @author irlan.gomes
 */
@ContentView(R.layout.result_view_pager)
public class PitagorasDetailActivity extends RoboFragmentActivity {

	/** The pager. */
	@InjectView(R.id.view_pager_pitagoras)
	private ViewPager pager;

	/** The adapter. */
	@Inject
	private PitagorasDetailPagerAdapter pagerAdapter;

	/** The is visible menu. */
	private boolean isVisibleMenu = true;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.result_view_pager);

		initAdapter();

	}

	/**
	 * Inits the adapter.
	 */
	private void initAdapter() {
		final List<ResultPitagorasDTO> valueResult = getIntent().getExtras()
				.getParcelableArrayList(TagConst.ACTION_DETAIL_NAME);

		isVisibleMenu(valueResult);
		pagerAdapter.setResult(valueResult);
		pager.setAdapter(pagerAdapter);
		pager.setOnPageChangeListener(doOnPageChangeListener());
	}

	/**
	 * Checks if is visible menu.
	 * 
	 * @param valueResult
	 *            the value result
	 */
	private void isVisibleMenu(final List<ResultPitagorasDTO> valueResult) {
		if (valueResult.size() == 1) {
			isVisibleMenu = false;
		}
	}

	/**
	 * Do on page change listener.
	 * 
	 * @return the simple on page change listener
	 */
	private SimpleOnPageChangeListener doOnPageChangeListener() {
		return new ViewPager.SimpleOnPageChangeListener() {
			@Override
			public void onPageSelected(final int position) {
				// When changing pages, reset the action bar actions since they
				// are dependent
				// on which page is currently active. An alternative approach is
				// to have each
				// fragment expose actions itself (rather than the activity
				// exposing actions),
				// but for simplicity, the activity provides the actions in this
				// sample.
				invalidateOptionsMenu();
			}
		};
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		super.onCreateOptionsMenu(menu);

		getMenuInflater().inflate(R.menu.activity_screen_slide, menu);

		setEnableMenuItem(menu);

		addTitleMenu(menu);

		return isVisibleMenu;
	}

	/**
	 * Enables the menu item when it is not the first page.
	 * 
	 * @param menu
	 *            the new enable menu item
	 */
	private void setEnableMenuItem(final Menu menu) {

		final MenuItem itemPrevious = menu
				.findItem(IdViewConst.ACTION_PREVIOUS);
		if (pager.getCurrentItem() > 0) {
			itemPrevious.setEnabled(true);
		} else {
			itemPrevious.setEnabled(false);
		}

	}

	/**
	 * Adds the title menu.
	 * 
	 * @param menu
	 *            the menu
	 */
	private void addTitleMenu(final Menu menu) {

		final MenuItem item = menu.add(Menu.NONE, IdViewConst.NEXT, Menu.NONE,
				IdViewConst.NEXT);

		item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
				| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
	 */
	@Override
	public boolean onOptionsItemSelected(final MenuItem menuItem) {

		switch (menuItem.getItemId()) {
		case IdViewConst.ACTION_PREVIOUS:
			setPagerItem(-1);

			if (pager.getCurrentItem() > 0) {
				menuItem.setEnabled(true);
			} else {
				menuItem.setEnabled(false);
			}

			return true;
		case IdViewConst.NEXT:
			setPagerItem(+1);

			// disables the next button at the last
			if (pager.getCurrentItem() != pagerAdapter.getCount() - 1) {
				menuItem.setEnabled(true);
			} else {
				menuItem.setEnabled(false);
			}
			return true;
		}

		return super.onOptionsItemSelected(menuItem);
	}

	/**
	 * Sets the pager item.
	 * 
	 * @param position
	 *            the new pager item
	 */
	private void setPagerItem(final int position) {
		pager.setCurrentItem(pager.getCurrentItem() + position);
	}

}
